#include "Factory_xNodeType.h" 
#include "xMappingNodeTypeLibrary.h"
#include "xNodeType_VM.h"
#include "xNodeType_WOP.h"
#include "xNodeType_wopcollector.h"
#include "xNodeType_vmcollector.h"
#include "xNodeType_gate.h"
#include "xNodeType_vmclone.h"
#include "xNodeType_Math.h"
#include "xNodeType_composite.h"
#include "xNodeType_Clock.h"
#include "xNodeType_StringComposer.h"
#include "xNodeType_existing.h"


xNodeType * Factory_xNodeType::create_xnodetype(string name, Factory_parameter fp) {
	int index = xMappingNodeTypeLibrary::getInstance()->get_xmnt_descriptor_index(name);
	return create_xnodetype(index,fp);
}

xNodeType * Factory_xNodeType::create_xnodetype(string name) {
	Factory_parameter fp;
	int index = xMappingNodeTypeLibrary::getInstance()->get_xmnt_descriptor_index(name);
	return create_xnodetype(index,fp);
}
xNodeType * Factory_xNodeType::create_xnodetype(int index, Factory_parameter fp) {
	
	xNodeType * nt ;

	// index is the array index of the xmnt_library
	xMappingNodeTypeDescriptor*  xmnt_des = xMappingNodeTypeLibrary::getInstance()->get_xmnt_descriptor(index);

	if (xmnt_des->_category == "ubvm"||xmnt_des->_category == "reference"||xmnt_des->_name == "WRAPPER_UBVM") {
		string vmname = xmnt_des->_idname ;
		nt = new xNodeType_VM( vmLibrary::instance().get_id_by_name( vmname ) );
	} else if (xmnt_des->_name == "wop") {
		assert(fp._type == Factory_parameter::WOP);
		nt = new xNodeType_WOP(fp._en);
	} else if (xmnt_des->_name == "variable_collector") {
		nt = new xNodeType_wopcollector();
	} else if (xmnt_des->_name == "vm_collector") {
		nt = new xNodeType_vmcollector();
	} else if (xmnt_des->_name.find("gate") != xmnt_des->_name.npos) {
		assert(fp._type == Factory_parameter::TYPE);
		nt = new xNodeType_gate(fp._typestring);
	} else if (xmnt_des->_name == "vmclone") {
		nt = new xNodeType_vmclone();
	} else if (xmnt_des->_name == "math") {
		nt = new xNodeType_Math();
	} else if (xmnt_des->_name == "string_composer"){
		nt = new xNodeType_StringComposer();		
	} else if (xmnt_des->_name == "clock"){
		nt = new xNodeType_Clock();		
	} else if (xmnt_des->_category == "composite") {
		nt = new xNodeType_composite(index);
	} else if ( fp._type == Factory_parameter::VM ) {
		nt = new xNodeType_existing( fp._vm );
	} else
		assert(false);

	nt->setInportFixed(xmnt_des->in_para_fixed);
	nt->setOutportFixed(xmnt_des->out_para_fixed);
	return nt ;

}

